package com.encode;

/**
 * Created by fly on 13/11/2017.
 */

public class DataTools {
    private volatile static DataTools instance;
    static {
        System.loadLibrary("native-lib");
    }
    private DataTools() {

    }

    //    private long crc=Long.parseLong("12345678",16);
    public static synchronized DataTools getInstance() {
        if (instance == null)
            synchronized (DataTools.class) {
                if (instance == null)
                    instance = new DataTools();
            }
        return instance;
    }

    private byte[] enCode = new byte[2];

    public void receive(byte[] bytes) {
        byte[] datas = new byte[bytes.length];
        if (bytes[0] == (byte) 0x55) {
            datas[0] = enCode[0];
            datas[1] = enCode[1];
            for (int i = 2; i < bytes.length; i++) {
                datas[i] = bytes[i];
            }

        }

    }
    /**
     *
     * @param phone    long pho=13532385018L;
     * @return
     */
    private byte[] phone(long phone) {
        return toBytes(5,phone);
    }
    private byte[] toBytes(int length,long data){
        byte[] b = new byte[length];
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) ((data >> ((b.length-1-i) * 8))& 0XFF);
        }
        return b;
    }

    //4
    public static final int PASSWORD=  0xb5;
    public byte[] sendPas(int command,long pasL,long phoneL){
        byte[] phone = phone(phoneL);
        byte[] pas = toBytes(3,pasL);
        int length=11;
        int[] data = new int[length + 4];
        data[0] = 0xcc;
        data[1] = 0x5e;
        data[2] = phone[4];
        data[3] = phone[3];
        data[4] = phone[2];
        data[5] = phone[1];
        data[6] = phone[0];

        data[7] = command;

        data[8] = pas[0];
        data[9] = pas[1];
        data[10] = pas[2];

        data=encode(data,0x12345678L);
        byte[] bytes= new byte[length+6];
        bytes[0]=(byte)0x55;
        bytes[1]=(byte)data.length;
        for (int i = 0 ; i < data.length;i++){
            bytes[i+2]=(byte)data[i];
        }
        return bytes;
    }
    //2
    public static final int FIGER_DEL=  0xbb;
    public byte[] sendFig(int command,int id,long phoneL){
        byte[] phone = phone(phoneL);

        int length=9;
        int[] data = new int[length + 4];
        data[0] = 0xcc;
        data[1] = 0x5e;
        data[2] = phone[4];
        data[3] = phone[3];
        data[4] = phone[2];
        data[5] = phone[1];
        data[6] = phone[0];

        data[7] = command;

        data[8] = id;

        data=encode(data,0x12345678L);
        byte[] bytes= new byte[length+6];
        bytes[0]=(byte)0x55;
        bytes[1]=(byte)data.length;
        for (int i = 0 ; i < data.length;i++){
            bytes[i+2]=(byte)data[i];
        }
        return bytes;
    }

    //5
    public static final int OPEN_AUTO=  0xa0;
    public static final int OPEN=  0xa1;
    public static final int LOCK=  0xa2;
    public static final int BLUE_OFF=  0xa3;
    public static final int PH_SETTING_ON=  0xb0;
    public static final int PHONE_DEL=  0xb2;
    /**
     * a0,a1,a2,a3,bo,b2  //5
     * @param command
     * @param phoneL
     * @return
     */
    public byte[] sendWPh(int command,long phoneL){
        byte[] phone =phone(phoneL);

        int[] data = new int[13 + 4];
        data[0] = 0xcc;
        data[1] = 0x5e;
        data[2] = phone[4];
        data[3] = phone[3];
        data[4] = phone[2];
        data[5] = phone[1];
        data[6] = phone[0];

        data[7] = command;

        data[8] = phone[4];
        data[9] = phone[3];
        data[10] = phone[2];
        data[11] = phone[1];
        data[12] = phone[0];
        data=encode(data,0x12345678L);
        byte[] bytes= new byte[19];
        bytes[0]=(byte)0x55;
        bytes[1]=(byte)data.length;
        for (int i = 0 ; i < data.length;i++){
            bytes[i+2]=(byte)data[i];
        }
        return bytes;
    }

    //6
    public static final int PHONE_ADD=  0xb1;
    /**
     * add phone //6
     * @param command
     * @param id (0~8)
     * @param phoneL
     * @return
     */
    public byte[] send(int command,int id,long phoneL){
        byte[] phone = phone(phoneL);
        int length=14;
        int[] data = new int[length + 4];
        data[0] = 0xcc;
        data[1] = 0x5e;
        data[2] = phone[4];
        data[3] = phone[3];
        data[4] = phone[2];
        data[5] = phone[1];
        data[6] = phone[0];

        data[7] = command;
        data[8] = id;

        data[9] = phone[4];
        data[10] = phone[3];
        data[11] = phone[2];
        data[12] = phone[1];
        data[13] = phone[0];
        data=encode(data,0x12345678L);
        byte[] bytes= new byte[length+6];
        bytes[0]=(byte)0x55;
        bytes[1]=(byte)data.length;
        for (int i = 0 ; i < data.length;i++){
            bytes[i+2]=(byte)data[i];
        }
        return bytes;
    }

    //1
    public static final int PHONE_CLEAR_NO1=  0xb3;
    public static final int PHONE_CLEAR_ALL=  0xb4;
    public static final int RECOVER=  0xb6;
    public static final int LOCK_STATUS_CHECK=  0xb7;
    public static final int ALARM_ENTER=  0xb8;
    public static final int ALARM_EXIT=  0xb9;
    public static final int FIGER_ADD=  0xba;
    public static final int FIGER_CLEAR=  0xbc;
    public static final int SETING_EXIT=  0xbd;
    /**
     * 1
     * @param command
     * @param phoneL
     * @return
     */
    public byte[] sendOne(int command,long phoneL){
        byte[] phone = phone(phoneL);
        int lenght=8;
        int[] data = new int[lenght + 4];
        data[0] = 0xcc;
        data[1] = 0x5e;
        data[2] = phone[4];
        data[3] = phone[3];
        data[4] = phone[2];
        data[5] = phone[1];
        data[6] = phone[0];
        data[7] = command;
        data=encode(data,0x12345678L);
        byte[] bytes= new byte[lenght+6];
        bytes[0]=(byte)0x55;
        bytes[1]=(byte)data.length;
        for (int i = 0 ; i < data.length;i++){
            bytes[i+2]=(byte)data[i];
        }
        return bytes;
    }

    public native int[] encode(int[] data,long psd);

    public native long phoneNumber(int[] data);

}
